package com.h3c.ptability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.custom.BusinessException;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.ptability.entity.PtRole;
import com.h3c.ptability.mapper.PtRoleMapper;
import com.h3c.ptability.service.IPtRoleService;
import com.h3c.ptability.dto.PtRoleDTO;
import com.h3c.ptability.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: PtRoleServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_role serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class PtRoleServiceImpl extends ServiceImpl<PtRoleMapper, PtRole> implements IPtRoleService {

    @Resource
    private PtRoleMapper ptRoleMapper;

    

    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtRoleDTO> selectFuzzyByDto(PtRoleDTO dto) {
        //List<PtRole> list = ptRoleMapper.selectByExample(initFuzzyExample(dto));
        List<PtRole> list = ptRoleMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),PtRole.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtRoleDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtRoleDTO> selectByDto(PtRoleDTO dto) {
        //List<PtRole> list = ptRoleMapper.selectByExample(initExample(dto));
        List<PtRole> list = ptRoleMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),PtRole.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), PtRoleDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(PtRoleDTO dto) {
        QueryWrapper<PtRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PtRole::getRoleId, dto.getRoleId()).eq(PtRole::getAppId, dto.getAppId());
        List<PtRole> roles = ptRoleMapper.selectList(queryWrapper);
        if (!roles.isEmpty()) {
            throw new BusinessException(StatusCodeEnum.ADD_FAIl.getCode(), "重复添加");
        }
        return ptRoleMapper.insertPtRole(JSONObject.parseObject(JSON.toJSONString(dto), PtRole.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(PtRoleDTO dto) {
        return ptRoleMapper.updatePtRole(JSONObject.parseObject(JSON.toJSONString(dto), PtRole.class));
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(PtRoleDTO dto) {
        return ptRoleMapper.deletePtRole(JSONObject.parseObject(JSON.toJSONString(dto), PtRole.class));
    }

    @Override
    public List<PtRoleDTO> queryList(PtRoleDTO paramsDTO) {
        QueryWrapper<PtRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PtRole::getAppId, paramsDTO.getAppId());
        List<PtRole> ptRoles = ptRoleMapper.selectList(queryWrapper);
        return JSONArray.parseArray(JSONArray.toJSONString(ptRoles), PtRoleDTO.class);
    }
}
